package com.business.activiti.listener;

import com.business.activiti.model.auto.AppealInfo;
import com.business.activiti.model.auto.TaskNode;
import com.business.activiti.service.AppealInfoService;
import com.business.activiti.service.TaskNodeService;
import com.business.appealtype.model.auto.AppealType;
import com.business.appealtype.service.AppealTypeService;
import com.business.customer.model.auto.CustomerInfo;
import com.business.customer.service.CustomerInfoService;
import com.business.system.model.auto.TsysRole;
import com.business.system.model.auto.TsysRoleExample;
import com.business.system.model.auto.TsysUser;
import com.business.system.model.auto.TsysUserExample;
import com.business.system.service.SysRoleService;
import com.business.system.service.SysUserService;
import com.business.weixin.util.WeixinTempleteMsg;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.activiti.engine.*;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class TaskCreateListener implements EventHandler {
    @Autowired
    private TaskNodeService taskNodeService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private AppealInfoService appealInfoService;

    @Autowired
    private AppealTypeService appealTypeService;
    @Autowired
    private CustomerInfoService customerInfoService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private WeixinTempleteMsg weixinTempleteMsg;

    @Override
    public void handle(ActivitiEvent event) {
        System.out.println("activiti任务创建");
        ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
        TaskEntity task = (TaskEntity) eventImpl.getEntity();
        TaskNode taskNode=new TaskNode();
        taskNode.setTaskId(task.getId());
        taskNode.setCreateTime(task.getCreateTime());
        taskNodeService.insertSelective(taskNode);
        Set<IdentityLink> candidates = task.getCandidates();
        List<String> roleIds=new ArrayList<String>();
        List<String> userIds=new ArrayList<String>();
        for (IdentityLink identityLink : candidates) {
            if(StringUtils.isNotBlank(identityLink.getGroupId())){
                roleIds.add(identityLink.getGroupId());
            }
            if(StringUtils.isNotBlank(identityLink.getUserId())){
                userIds.add(identityLink.getUserId());
            }
        }
        /*List<TsysRole> roleList=new ArrayList<>();
        if(roleIds.size()>0){
            TsysRoleExample roleExample=new TsysRoleExample();
            roleExample.createCriteria().andIdIn(roleIds);
            roleList=sysRoleService.selectByExample(roleExample);
        }


        List<TsysUser> userList=new ArrayList<>();
        if(userIds.size()>0){
            TsysUserExample userExample=new TsysUserExample();
            userExample.createCriteria().andIdIn(userIds);
            userList=sysUserService.selectByExample(userExample);
        }
        for(TsysRole role:roleList){
            System.out.println("角色："+role.getName());
        }
        for(TsysUser user:userList){
            System.out.println("用户："+user.getNickname());
        }*/
        //对以上角色和用户进行通知
        //此处是发送微信模版消息提醒
        //所需信息 openid 发起人 当前环节 流程名称

        ProcessInstance pi=task.getProcessInstance();
        //ProcessInstance pi=runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
        if(pi!=null){
            String bussinessKey=pi.getBusinessKey();
            AppealInfo appealInfo=appealInfoService.selectByPrimaryKey(bussinessKey);
            if(appealInfo!=null){
                List<TsysUser> userList=sysUserService.getUsersByRoleIdsAndUserIds(roleIds,userIds);
                if(userList!=null&&userList.size()>0){
                    TsysUser createUser=sysUserService.selectByPrimaryKey(appealInfo.getUserId());
                    AppealType at=appealTypeService.selectByPrimaryKey(appealInfo.getAppealTypeId());
                    String taskName=task.getName();
                    for(TsysUser user:userList){
                        CustomerInfo customerInfo=customerInfoService.getUniqueCustomerByUserId(user.getId());
                        if(customerInfo!=null&&StringUtils.isNotBlank(customerInfo.getOpenid())){
                            weixinTempleteMsg.sendTemplete_01(customerInfo.getOpenid(),createUser.getNickname(),task.getName(),at.getName(),pi.getId());
                        }
                    }
                }
            }
        }
    }
}
